package org.datascooter.db.mssql;

import java.text.MessageFormat;

import org.datascooter.bundle.attribute.PersistAttribute;
import org.datascooter.exception.EntityNotMappedException;
import org.datascooter.impl.DataSnip;
import org.datascooter.impl.SnipBuilder;
import org.datascooter.inface.ISnipBuilder;
import org.datascooter.meta.Column;
import org.datascooter.utils.DBType;
import org.datascooter.utils.DSSettings;
import org.datascooter.utils.SnipUtils;

public class MssqlSnipBuilder extends SnipBuilder {

	public MssqlSnipBuilder() {
		super("mssql");
	}

	@Override
	public ISnipBuilder copy() {
		return new MssqlSnipBuilder();
	}

	@Override
	protected String buildDelete(DataSnip dataSnip) throws EntityNotMappedException {
		buildWhereClause(dataSnip);
		return MessageFormat.format(SnipUtils.DELETE_FROM1, getCaseSensitiveTableName(dataSnip.bundle.getTableName()), SnipUtils.NN, where.toString());
	}

	@SuppressWarnings("nls")
	@Override
	public String buildColumnClause(PersistAttribute attribute) {
		String typeStr = getDbTypeString(attribute.getType());
		String size = buildDataLength(attribute.getScale(), attribute.getPrecision());
		DBType type = attribute.getType();
		switch (type) {
			case CHAR:
				typeStr = SnipUtils.CHAR;
				if (size.length() == 0) {
					size = "(255)";
				}
				break;
			case TIMESTAMP:
				typeStr = "DATETIME";
				size = "";
				break;
			case BIGDECIMAL:
				typeStr = "DECIMAL";
				size = "(22,2)";
				break;
			case LONG:
				typeStr = "BIGINT";
				size = "";
				break;
			case BOOLEAN:
				typeStr = "BIT";
				size = "";
				break;
			case INT:
				typeStr = "INT";
				size = "";
				break;
			case STRING:
				typeStr = "VARCHAR";
				if (size.length() == 0) {
					size = "(" + DSSettings.getDefaultStringLength() + ")";
				}
				break;
			case BLOB:
				typeStr = "IMAGE";
				size = "";
				break;
			case CLOB:
				typeStr = "NTEXT";
				size = "";
				break;
		}
		return SnipUtils.SP + attribute.getColumnName() + SnipUtils.SP + typeStr + SnipUtils.SP + size + (attribute.isNullable() ? SnipUtils.NN : SnipUtils.NOT_NULL);
	}

	@Override
	public boolean equalType(PersistAttribute attribute, Column column) {
		boolean result = false;
		boolean scaled = true;
		if (attribute.getType() == null) {
			return true;
		}
		switch (attribute.getType()) {
			case BIGDECIMAL:
				result = "DECIMAL".equalsIgnoreCase(column.dataTypeName);
				break;
			case LONG:
				result = "BIGINT".equalsIgnoreCase(column.dataTypeName);
				scaled = false;
				break;
			case BOOLEAN:
				result = "bit".equalsIgnoreCase(column.dataTypeName);
				break;
			case INT:
				result = "int".equalsIgnoreCase(column.dataTypeName);
				scaled = false;
				break;
			case STRING:
				result = "VARCHAR".equalsIgnoreCase(column.dataTypeName);
				break;
			case CHAR:
				result = "CHAR".equalsIgnoreCase(column.dataTypeName);
				break;
			case BLOB:
				result = "BLOB".equalsIgnoreCase(column.dataTypeName);
				break;
			case CLOB:
				result = "CLOB".equalsIgnoreCase(column.dataTypeName);
				break;
			case TIMESTAMP:
				result = "datetime".equalsIgnoreCase(column.dataTypeName);
				scaled = false;
				break;
		}
		result = checkScale(attribute, column, result, scaled);
		return result;
	}
}
